package com.mto.module.karaoke.portlet.util.comparator;

import java.util.Date;

import com.liferay.portal.kernel.util.OrderByComparator;
import com.mto.module.karaoke.model.Song;

public class ModifiedDateComparator extends OrderByComparator {

	private static final long serialVersionUID = -1523121635297308246L;
	public static final String ORDER_BY_ASC = "modifiedDate ASC";
	public static final String ORDER_BY_DESC = "modifiedDate DESC";
	public static final String[] ORDER_BY_FIELDS = { "modifiedDate" };

	public ModifiedDateComparator() {
		this(false);
	}

	public ModifiedDateComparator(boolean ascending) {
		_ascending = ascending;
	}

	@Override
	public int compare(Object obj1, Object obj2) {
		int value = 0;
		Song song1 = (Song) obj1;
		Song song2 = (Song) obj2;
		Date date1 = song1.getModifiedDate();
		Date date2 = song2.getModifiedDate();
		if(date1 == null && date2 != null) value = -1;
		else if(date2 == null && date1 != null) value = 1;
		else if(date2 == null && date1 == null) value = 0;
		else value = date1.compareTo(date2);
		if (_ascending) {
			return value;
		} else {
			return -value;
		}
	}

	@Override
	public String getOrderBy() {
		if (_ascending) {
			return ORDER_BY_ASC;
		} else {
			return ORDER_BY_DESC;
		}
	}

	@Override
	public String[] getOrderByFields() {
		return ORDER_BY_FIELDS;
	}

	@Override
	public boolean isAscending() {
		return _ascending;
	}

	private boolean _ascending;
}
